package com.work.user.controller;

import com.github.pagehelper.Page;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import com.work.user.pojo.Menu;
import com.work.user.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    //根据id查询权限
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,StatusCode.FIND_Ok,"查询成功",menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.FIND_ERROR,"查询失败");
        }
    }

    //查询全部权限
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menus = menuService.findAll();
            return new Result(true,StatusCode.FIND_Ok,"查询成功",menus);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.FIND_ERROR,"查询失败");
        }
    }

    //分页查询所有用户
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        try {
            Page<Menu> pageList = menuService.findPage(searchMap, page, size);
            PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return new Result(true, StatusCode.FIND_Ok, "查询成功", pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.FIND_ERROR,"查询失败");
        }
    }

    //多条件搜索用户数据
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        try {
            List<Menu> list = menuService.findList(searchMap);
            return new Result(true, StatusCode.FIND_Ok, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.FIND_ERROR, "查询失败");
        }
    }

    //新增权限
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,StatusCode.ADD_OK,"新增成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ADD_ERROR,"新增失败");
        }
    }

    //修改权限
    @PutMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,StatusCode.UPDATE_Ok,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.UPDATE_ERROR,"修改失败");
        }
    }

    //删除权限
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,StatusCode.DELETE_OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.DELETE_ERROR,"删除失败");
        }
    }

}
